namespace PugLib.UnitTest
{
    #region

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Windows.Forms;
    using Extensions;
    using Extensions.Collections;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    #endregion

    /// <summary>
    ///This is a test class for PugExtensionsTest and is intended
    ///to contain all PugExtensionsTest Unit Tests
    ///</summary>
    [TestClass]
    public class PugExtensionsTest
    {
        private Label _control;

        /// <summary>
        ///A test for Map
        ///</summary>
        public void MapTestHelper()
        {
            List<int> list = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Func<int, int> func = x => (x * x);
            string expected = new List<int> { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 }.EnumerableToString();
            string actual = list.Map(func).EnumerableToString();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void MapTest()
        {
            MapTestHelper();
        }

        /// <summary>
        ///A test for ParallelMap
        ///</summary>
        public void ParallelMapTestHelper()
        {
            List<int> list = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Func<int, int> func = x => (x * x);
            string expected = new List<int> { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 }.EnumerableToString();
            string actual = list.ParallelMap(func).EnumerableToString();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ParallelMapTest()
        {
            ParallelMapTestHelper();
        }

        /// <summary>
        ///A test for To
        ///</summary>
        public void ToTestHelper()
        {
            IConvertible s = 8;
            const double expected = 8;
            double actual = s.To<double>();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ToTest()
        {
            ToTestHelper();
        }


        /// <summary>
        ///A test for IsNull
        ///</summary>
        [TestMethod]
        public void IsNullTest()
        {
            object input = null;
            Assert.IsTrue(input.IsNull());
            input = DBNull.Value;
            Assert.IsTrue(input.IsNull());

            input = 3;
            Assert.IsFalse(input.IsNull());
        }


        [TestMethod]
        public void InvokeIfRequiredSameThreadTest()
        {
            const string argument = "invoke test 1";
            _control = new Label();
            _control.BeginInvokeIfRequired(SetText, argument);
        }


        [TestMethod]
        public void InvokeIfRequiredOtherThreadTest()
        {
            const string argument = "invoke test 1";
            _control = new Label();
            ThreadPool.QueueUserWorkItem(InvokeIfRequiredOtherThreadTestHelper, argument);
        }

        private void InvokeIfRequiredOtherThreadTestHelper(object message)
        {
            _control.BeginInvokeIfRequired(SetText, message as string);
        }

        private void SetText(string text)
        {
            _control.Text = text;
        }


        [TestMethod]
        public void IterateIndexTestHelper()
        {
            Enumerable.Range(1, 10).ToArray().IterateIndex((i, j) => Console.WriteLine("{0}-{1}", i, j));
        }
    }
}